﻿using System;
using System.Drawing;
using System.Windows.Forms;
using Log;

namespace ilibrary.source.views
{
    public partial class TextEditor : UserControl
    {
        private EventLog log = new EventLog("ILibrary", "TextEditor");

        public event EventHandler Save;

        #region Constructors
        /// <summary>
        /// Constructeur par defaut sans initialisation du texte
        /// </summary>
        public TextEditor()
        {
            InitializeComponent();

            initFontFamily();
            initFontSize();

            this.richTextBox.Font =
                new Font(this.toolStripComboBoxFamily.SelectedItem.ToString(),
                    float.Parse(this.toolStripComboBoxSize.Text));
        }

        /// <summary>
        /// Constructeur chargeant le text initialement
        /// </summary>
        /// <param name="richText"></param>
        public TextEditor(string richText)
        {
            InitializeComponent();

            initFontFamily();
            initFontSize();

            this.richTextBox.Font =
                new Font(this.toolStripComboBoxFamily.SelectedItem.ToString(),
                    float.Parse(this.toolStripComboBoxSize.Text));

            this.richTextBox.Rtf = richText;
            this.richTextBox.Modified = false;
        }

        private void initFontSize()
        {
            try
            {
                this.toolStripComboBoxSize.BeginUpdate();
                this.toolStripComboBoxSize.SelectedIndexChanged -=
                    new EventHandler(toolStripComboBoxSize_SelectedIndexChanged);

                this.toolStripComboBoxSize.Items.Add(8);
                this.toolStripComboBoxSize.Items.Add(9);
                this.toolStripComboBoxSize.Items.Add(10);
                this.toolStripComboBoxSize.Items.Add(11);
                this.toolStripComboBoxSize.Items.Add(12);
                this.toolStripComboBoxSize.Items.Add(14);
                this.toolStripComboBoxSize.Items.Add(16);
                this.toolStripComboBoxSize.Items.Add(18);
                this.toolStripComboBoxSize.Items.Add(20);
                this.toolStripComboBoxSize.Items.Add(22);
                this.toolStripComboBoxSize.Items.Add(24);
                this.toolStripComboBoxSize.Items.Add(26);
                this.toolStripComboBoxSize.Items.Add(28);
                this.toolStripComboBoxSize.Items.Add(48);
                this.toolStripComboBoxSize.Items.Add(72);

                this.toolStripComboBoxSize.SelectedItem = 12;
            }
            catch (Exception ex)
            {
                log.Error("", ex);
            }
            finally
            {
                this.toolStripComboBoxSize.SelectedIndexChanged +=
                    new EventHandler(toolStripComboBoxSize_SelectedIndexChanged);
                this.toolStripComboBoxSize.EndUpdate();
            }
        }

        private void initFontFamily()
        {
            try
            {
                this.toolStripComboBoxFamily.BeginUpdate();
                this.toolStripComboBoxFamily.SelectedIndexChanged -=
                    new EventHandler(toolStripComboBoxFamily_SelectedIndexChanged);

                foreach (FontFamily font in FontFamily.Families)
                    this.toolStripComboBoxFamily.Items.Add(font.Name);

                this.toolStripComboBoxFamily.SelectedItem = "Arial";
            }
            catch (Exception ex)
            {
                log.Error("", ex);
            }
            finally
            {
                this.toolStripComboBoxFamily.SelectedIndexChanged +=
                    new EventHandler(toolStripComboBoxFamily_SelectedIndexChanged);
                this.toolStripComboBoxFamily.EndUpdate();
            }
        }
        #endregion

        public string RichText
        {
            get { return this.richTextBox.Rtf; }
        }

        public bool Modified
        {
            get { return this.richTextBox.Modified; }
            set { this.richTextBox.Modified = value; }
        }

        #region Save
        private void toolStripButtonSave_Click(object sender, EventArgs e)
        {
            try
            {
                Save(this, new EventArgs());
            }
            catch (Exception ex)
            {
                log.Error("", ex);
            }
        }
        #endregion

        #region Cut Copy Paste
        private void toolStripButtonCut_Click(object sender, EventArgs e)
        {
            try
            {
                this.richTextBox.Cut();
            }
            catch (Exception ex)
            {
                log.Error("", ex);
            }
        }

        private void toolStripButtonCopy_Click(object sender, EventArgs e)
        {
            try
            {
                this.richTextBox.Copy();
            }
            catch (Exception ex)
            {
                log.Error("", ex);
            }
        }

        private void toolStripButtonPaste_Click(object sender, EventArgs e)
        {
            try
            {
                this.richTextBox.Paste();
            }
            catch (Exception ex)
            {
                log.Error("", ex);
            }
        }
        #endregion

        #region Cancel Restore
        private void toolStripButtonCancel_Click(object sender, EventArgs e)
        {
            try
            {
                this.richTextBox.Undo();
            }
            catch (Exception ex)
            {
                log.Error("", ex);
            }
        }

        private void toolStripButtonRestore_Click(object sender, EventArgs e)
        {
            try
            {
                this.richTextBox.Redo();
            }
            catch (Exception ex)
            {
                log.Error("", ex);
            }
        }
        #endregion

        #region Bold Italic Underline
        private void toolStripButtonBold_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (toolStripButtonBold.Checked)
                    this.richTextBox.SelectionFont =
                        new Font(this.richTextBox.SelectionFont, this.richTextBox.SelectionFont.Style | FontStyle.Bold);
                else
                    this.richTextBox.SelectionFont =
                        new Font(this.richTextBox.SelectionFont, this.richTextBox.SelectionFont.Style & FontStyle.Italic & FontStyle.Underline);
            }
            catch (Exception ex)
            {
                log.Error("", ex);
            }
        }

        private void toolStripButtonItalic_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (toolStripButtonItalic.Checked)
                    this.richTextBox.SelectionFont =
                        new Font(this.richTextBox.SelectionFont, this.richTextBox.SelectionFont.Style | FontStyle.Italic);
                else
                    this.richTextBox.SelectionFont =
                        new Font(this.richTextBox.SelectionFont, this.richTextBox.SelectionFont.Style & FontStyle.Bold & FontStyle.Underline);
            }
            catch (Exception ex)
            {
                log.Error("", ex);
            }
        }

        private void toolStripButtonUnderline_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (toolStripButtonUnderline.Checked)
                    this.richTextBox.SelectionFont =
                        new Font(this.richTextBox.SelectionFont, this.richTextBox.SelectionFont.Style | FontStyle.Underline);
                else
                    this.richTextBox.SelectionFont =
                        new Font(this.richTextBox.SelectionFont, this.richTextBox.SelectionFont.Style & FontStyle.Bold & FontStyle.Italic);
            }
            catch (Exception ex)
            {
                log.Error("", ex);
            }
        }
        #endregion

        #region Left Center Right
        private void toolStripButtonLeft_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (this.toolStripButtonLeft.Checked)
                {
                    this.toolStripButtonCenter.Checked = false;
                    this.toolStripButtonRight.Checked = false;
                    this.richTextBox.SelectionAlignment = HorizontalAlignment.Left;
                }
            }
            catch (Exception ex)
            {
                log.Error("", ex);
            }
        }

        private void toolStripButtonCenter_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (this.toolStripButtonCenter.Checked)
                {
                    this.toolStripButtonLeft.Checked = false;
                    this.toolStripButtonRight.Checked = false;
                    this.richTextBox.SelectionAlignment = HorizontalAlignment.Center;
                }
            }
            catch (Exception ex)
            {
                log.Error("", ex);
            }
        }

        private void toolStripButtonRight_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (this.toolStripButtonRight.Checked)
                {
                    this.toolStripButtonLeft.Checked = false;
                    this.toolStripButtonCenter.Checked = false;
                    this.richTextBox.SelectionAlignment = HorizontalAlignment.Right;
                }
            }
            catch (Exception ex)
            {
                log.Error("", ex);
            }
        }
        #endregion

        #region Font
        private void toolStripComboBoxFamily_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                Font font = new Font((string) this.toolStripComboBoxFamily.SelectedItem,
                    this.richTextBox.SelectionFont.Size,
                    this.richTextBox.SelectionFont.Style);
                this.richTextBox.SelectionFont = font;
            }
            catch (Exception ex)
            {
                log.Error("", ex);
            }
        }

        private void toolStripComboBoxSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                Font font = new Font(this.richTextBox.SelectionFont.Name,
                    (int) this.toolStripComboBoxSize.SelectedItem,
                    this.richTextBox.SelectionFont.Style);
                this.richTextBox.SelectionFont = font;
            }
            catch (Exception ex)
            {
                log.Error("", ex);
            }
        }

        private void toolStripButtonColor_Click(object sender, EventArgs e)
        {
            try
            {
                ColorDialog colorDialog = new ColorDialog();
                if (colorDialog.ShowDialog() == DialogResult.OK)
                {
                    this.richTextBox.SelectionColor = colorDialog.Color;
                }
            }
            catch (Exception ex)
            {
                log.Error("", ex);
            }
        }
        #endregion

        #region RichTextBoxEvents
        private void richTextBox_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                // Suspension du traitement de ces évènements
                this.toolStripButtonBold.CheckedChanged -=
                    new System.EventHandler(this.toolStripButtonBold_CheckedChanged);
                this.toolStripButtonItalic.CheckedChanged -=
                    new System.EventHandler(this.toolStripButtonItalic_CheckedChanged);
                this.toolStripButtonUnderline.CheckedChanged -=
                    new System.EventHandler(this.toolStripButtonUnderline_CheckedChanged);

                this.toolStripButtonBold.Checked = this.richTextBox.SelectionFont.Bold;
                this.toolStripButtonItalic.Checked = this.richTextBox.SelectionFont.Italic;
                this.toolStripButtonUnderline.Checked = this.richTextBox.SelectionFont.Underline;

                switch (this.richTextBox.SelectionAlignment)
                {
                    case HorizontalAlignment.Left:
                        this.toolStripButtonLeft.Checked = true;
                        break;
                    case HorizontalAlignment.Center:
                        this.toolStripButtonCenter.Checked = true;
                        break;
                    case HorizontalAlignment.Right:
                        this.toolStripButtonRight.Checked = true;
                        break;
                }

                this.toolStripComboBoxFamily.SelectedItem = this.richTextBox.SelectionFont.FontFamily.Name;
                this.toolStripComboBoxSize.SelectedItem = int.Parse(this.richTextBox.SelectionFont.Size.ToString());
                this.toolStripComboBoxSize.Text = int.Parse(this.richTextBox.SelectionFont.Size.ToString()).ToString();

                if (string.Compare(this.richTextBox.SelectedText, string.Empty) == 0)
                {
                    this.toolStripButtonCut.Enabled = false;
                    this.toolStripButtonCopy.Enabled = false;
                }
                else
                {
                    this.toolStripButtonCut.Enabled = true;
                    this.toolStripButtonCopy.Enabled = true;
                }
            }
            catch (Exception ex)
            {
                log.Error("", ex);
            }
            finally
            {
                this.toolStripButtonBold.CheckedChanged +=
                    new System.EventHandler(this.toolStripButtonBold_CheckedChanged);
                this.toolStripButtonItalic.CheckedChanged +=
                    new System.EventHandler(this.toolStripButtonItalic_CheckedChanged);
                this.toolStripButtonUnderline.CheckedChanged +=
                    new System.EventHandler(this.toolStripButtonUnderline_CheckedChanged);
            }
        }
        #endregion
    }
}
